let express = require("express");
let dynamic = express.Router();
let {pool} = require('../db/pool')
let fs = require('fs')
// let qs = require('querystring')
dynamic.get('/dynamic/findAll',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = 'select * from dynamic_recent,base_user where dynamic_recent.author_id=base_user.user_id order by create_time desc'
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            res.send(result)
            connection.release() 
        })
    })
})
dynamic.get('/dynamic/findOne',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select * from dynamic_recent,base_user where dynamic_recent.author_id=base_user.user_id and dynamic_recent.dy_id=${req.query.dy_id}`
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            res.send(result[0])
            connection.release() 
            // console.log(result[0])
        })
    })
})
dynamic.get('/dynamic/write',(req,res)=>{
    // let user_id = 'wo'
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        console.log(req.query.username)
        let sql = `select user_id from base_user where user_name='${req.query.username}'`
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            // console.log(result[0].user_id)  
            let dy_id = Math.random()*100000000
            let sql = `insert into dynamic_recent (dy_id,author_id,content,create_time,comment,good,forwarded) values ('${dy_id}','${result[0].user_id}','${req.query.message}','${req.query.time}',0,0,0)`;
            connection.query(sql,(err,result)=>{
                if(err) throw err;
                // console.log(result)
                res.send(result)
                connection.release() 
            })
        })
    })
 
})
dynamic.get('/dynamic/forward',(req,res)=>{
    // console.log(req.query)
    // res.send(req.query)
    pool.getConnection((err,connection)=>{
    if(err) throw err;
    console.log(req.query.username)
        let sql = `select user_id from base_user where user_name='${req.query.username}'`
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            // console.log(result[0].user_id)  
            let dy_id = Math.random()*100000000
            let sql1 = `insert into dynamic_recent (dy_id,author_id,content,create_time,forward_dy_id,forwarded,good,comment) 
            values ('${dy_id}','${result[0].user_id}','${req.query.message}','${req.query.time}','${req.query.dy_id}',0,0,0)`;
            connection.query(sql1,(err,result)=>{
                if(err) throw err;
                console.log(result)
                res.send(result)
            })
            // console.log(req.query.dy_id)
            let sql2 = `select forwarded from dynamic_recent where dy_id='${req.query.dy_id}'`
            connection.query(sql2,(err,result)=>{
                // console.log(result.forwarded)
                let forward = result[0].forwarded
                // console.log(result.forwarded)
                forward += 1;
                // console.log(forward)
                let sql3 = `update dynamic_recent set forwarded='${forward}' where dy_id='${req.query.dy_id}'`
                connection.query(sql3,(err,result)=>{
                    if(err) throw err;
                    // console.log(result)
                })
            })
            connection.release() 
        })
    })
})
dynamic.get('/dynamic/good',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        console.log(req.query)
        let sql = `select good from dynamic_recent where dy_id='${req.query.dy_id}'`
        connection.query(sql,(err,result)=>{
            if (err) throw err;
            let good = result[0].good
            good += 1;
            let sql2 = `update dynamic_recent set good='${good}' where dy_id='${req.query.dy_id}'`
            connection.query(sql2,(err,result)=>{
                // console.log(result)
                res.send(result)
            })
            connection.release() 
        })
    })
})
dynamic.post('/dynamic/detail',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select * from dynamic_recent,base_user where base_user.user_id=dynamic_recent.author_id and dy_id='${req.body.dy_id} and ,'`
        connection.query(sql,(err,resultMain)=>{
            if(err) throw err;
            res.send(resultMain)
        })
        connection.release();
    })
})
dynamic.get('/dynamic/findComment',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select * from comment_tb where dy_id='${req.query.dy_id}'`
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            res.send(result)
        })
        connection.release();
    })
})
dynamic.get('/dynamic/deleteById',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `delete from dynamic_recent where dy_id='${req.query.dy_id}'`
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            res.send(result)
        })
    })
})
module.exports = {
    dynamic
}